<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <!-- <a-form-item label="物料id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielId']" placeholder="请输入物料id"></a-input>
        </a-form-item> -->
        <!-- <a-form-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['materielType']" placeholder="请输入物料类型"></a-input>
        </a-form-item> -->
        <a-form-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['materielType', validatorRules.materialType]" :trigger-change="true"
            dictCode="material_type" placeholder="请选择料件类型" />
        </a-form-item>
        <a-form-item label="内部料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['materielCode']" placeholder="请输入内部料号"></a-input> -->
          <j-popup v-decorator="['materielCode', validatorRules.materielCode]" :trigger-change="true" org-fields="id,materiel_code,product_name,gauge,materiel_type,unit"
            dest-fields="materielId,materielCode,materielName,materielGauge,materialType,countUnit" code="materiel_search"
            @callback="popupCallback" />
        </a-form-item>
        <a-form-item label="品名" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['materielName']" placeholder="请输入品名"></a-input> -->
          <a-input v-decorator="['materielName', validatorRules.materielName]" :trigger-change="true" placeholder="请输入物料名称" disabled></a-input>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['materielGauge']" placeholder="请输入规格"></a-input> -->
          <a-input v-decorator="['materielGauge', validatorRules.materielGauge]" :trigger-change="true" placeholder="请输入物料规格" disabled></a-input> 
        </a-form-item>        
        <a-form-item label="计量单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <!-- <a-input v-decorator="['countUnit']" placeholder="请输入计量单位"></a-input> -->
          <a-input v-decorator="['countUnit', validatorRules.unit]" :trigger-change="true" placeholder="请输入单位"></a-input>
        </a-form-item>
        <a-form-item label="需求数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['requireNum']" placeholder="请输入需求数量"></a-input>
        </a-form-item>
        <a-form-item label="提货数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['pickupNum']" placeholder="请输入提货数量"></a-input>
        </a-form-item>
        <a-form-item label="承诺数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['commitNu']" placeholder="请输入承诺数量"></a-input>
        </a-form-item>
        <a-form-item v-show="false" label="工厂ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-show="false" v-decorator="['factoryId']" placeholder="请输入工厂ID"></a-input>
        </a-form-item>
        <a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<j-popup v-decorator="['factoryCode',validatorRules.factoryCode]" :trigger-change="true" org-fields="id,fac_code,fac_nameone,store_name"
        	 dest-fields="factoryId,factoryCode,factoryName,storageSite" code="factorysite_search" @callback="popupCallback" />
        </a-form-item>
        <a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['factoryName']" placeholder="请输入工厂名称"disabled></a-input>
        </a-form-item>
        <a-form-item label="库存地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
        	<a-input v-decorator="['storageSite']" placeholder="请输入库存地点"disabled></a-input>
        </a-form-item>
        <!-- <a-form-item label="工厂id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['factoryId']" placeholder="请输入工厂id"></a-input>
        </a-form-item> -->
        <!-- <a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['factoryCode']" placeholder="请输入工厂编号"></a-input>
        </a-form-item>
        <a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['factoryName']" placeholder="请输入工厂名称"></a-input>
        </a-form-item>
        <a-form-item label="库存地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['storageSite']" placeholder="请输入库存地点"></a-input>
        </a-form-item> -->
        <!-- <a-form-item label="正面用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['frontQuantity']" placeholder="请输入正面用量"></a-input>
        </a-form-item> -->
        <a-form-item label="库存数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['stockNum']" placeholder="请输入库存数量"></a-input>
        </a-form-item>
        <a-form-item label="已发料数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['deliveryNum']" placeholder="请输入已发料数量"></a-input>
        </a-form-item>
        <a-form-item label="已上料数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['unglazeNum']" placeholder="请输入已上料数量"></a-input>
        </a-form-item>
        <a-form-item label="已退料数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['withdrawNum']" placeholder="请输入已退料数量"></a-input>
        </a-form-item>
        <a-form-item label="发料状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['ifDelivery']" placeholder="请输入发料状态"></a-input>
        </a-form-item>
        <a-form-item label="上料状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['ifGlaze']" placeholder="请输入上料状态"></a-input>
        </a-form-item>
        <a-form-item label="真实数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['realNum']" placeholder="请输入真实数量"></a-input>
        </a-form-item>
        <!-- <a-form-item label="生产订单id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['proorderId']" placeholder="请输入生产订单id" disabled></a-input>
        </a-form-item>
        <a-form-item label="生产订单子表id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['orderitemId']" placeholder="请输入生产订单子表id" disabled></a-input>
        </a-form-item>
        <a-form-item label="制令单id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['commandbillId']" placeholder="请输入制令单id" disabled></a-input>
        </a-form-item>
        <a-form-item label="制令单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['commandbillCode']" placeholder="请输入制令单号" disabled></a-input>
        </a-form-item> -->
        <a-form-item label="用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['quantity']" placeholder="请输入用量"></a-input>
        </a-form-item>
         <!--<a-form-item label="建议退料数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['adviceNum']" placeholder="请输入建议退料数量"></a-input>
        </a-form-item>
        <a-form-item label="实际退料数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['virtualNum']" placeholder="请输入实际退料数量"></a-input>
        </a-form-item>
        <a-form-item label="消耗数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['consumeNum']" placeholder="请输入消耗数量"></a-input>
        </a-form-item>
        <a-form-item label="转产数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['transformNum']" placeholder="请输入转产数量"></a-input>
        </a-form-item> -->

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "MesCommandbillPitemModal",
    components: {
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          materialType: {
            rules: [{
              required: true,
              message: '请输入料件类型!'
            }, ]
          },
          unit: {
            rules: [{
              required: true,
              message: '请输入单位!'
            }, ]
          },
          materielName: {
            rules: [{
              required: true,
              message: '请输入品名!'
            }, ]
          },
          materielGauge: {
            rules: [{
              required: true,
              message: '请输入料件规格!'
            }, ]
          },
          materielCode: {
            rules: [{
              required: true,
              message: '请选择内部料号!'
            }, ]
          },
          factoryCode: {
          	rules: [{
          		required: true,
          		message: '请选择工厂编号!'
          	}, ]
          },
        },
        url: {
          add: "/produce/mesCommandbillInfo/addMesCommandbillPitem",
          edit: "/produce/mesCommandbillInfo/editMesCommandbillPitem",
        }

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','updateBy','updateTime','sysOrgCode','materielId','materielType','materielCode','materielName','materielGauge','requireNum','countUnit','pickupNum','commitNu','factoryId','factoryCode','factoryName','storageSite','frontQuantity','stockNum','deliveryNum','unglazeNum','withdrawNum','ifDelivery','ifGlaze','realNum','proorderId','orderitemId','commandbillId','commandbillCode','quantity','adviceNum','virtualNum','consumeNum','transformNum'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData['commandbillId'] = this.mainId
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','sysOrgCode','materielId','materielType','materielCode','materielName','materielGauge','requireNum','countUnit','pickupNum','commitNu','factoryId','factoryCode','factoryName','storageSite','frontQuantity','stockNum','deliveryNum','unglazeNum','withdrawNum','ifDelivery','ifGlaze','realNum','proorderId','orderitemId','commandbillId','commandbillCode','quantity','adviceNum','virtualNum','consumeNum','transformNum'))
      },


    }
  }
</script>
